<template>
    <div class="apiCacheContainer">
        <div class="searchContainer">
            <ServiceConfigSearch />
        </div>
        <div class="insertContainer">
            <el-button type="primary" plain size="small" icon="PlusOutlined" @click="addConfig()">添 加</el-button>
            <el-button type="primary" plain icon="PlusOutlined" @click="refreshConfig()" style="float: right">刷新配置</el-button>
        </div>
        <div>
            <el-table
                :data="configCenterDataList.dataList"
                style="width: 100%; margin-bottom: 20px; margin-top: 20px;"
                row-key="id"
                border
                :stripe="true"
                :show-overflow-tooltip="true"
                class="tableContainer"
                max-height="70vh"
            >
                <el-table-column prop="application" label="微服务ID" />
                <el-table-column prop="profile" label="微服务profile" />
                <el-table-column prop="key" label="键" />
                <el-table-column prop="value" label="值" />
                <el-table-column align="center">
                    <template #header>
                        <span>操 作</span>
                    </template>
                    <template #default="scope">
                        <el-button
                            size="small"
                            type="success"
                            @click="handleUpdate(scope.$index, scope.row)">修 改</el-button>
                        <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删 除</el-button>
                        <el-button type="primary" size="small" @click="handleCopy(scope.$index, scope.row)">复 制</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                v-model:current-page="configCenterDataList.pageIndex"
                v-model:page-size="configCenterDataList.pageSize"
                :small="false"
                :background="true"
                layout="prev, pager, next, jumper, sizes, total"
                :total="configCenterDataList.recordCount"
                @change="changePage"
                :page-sizes="[10, 20, 30, 50, 100, 200, 400]"
                style="float: right"
            />
        </div>

        <div>
            <ServiceAddOrUpdateConfigDialog :configCenterForm="configCenterForm"/>
        </div>
        <div>
            <ServiceDeleteConfigDialog :configCenterForm="configCenterForm"/>
        </div>
        <div>
            <ServiceRefreshConfigCenterDialog />
        </div>
    </div>
</template>

<script>
import ServiceConfigSearch from "@/components/serviceManage/serviceConfigManage/ServiceConfigSearch";
import ServiceAddOrUpdateConfigDialog from "@/components/serviceManage/serviceConfigManage/ServiceAddOrUpdateConfigDialog";
import ServiceDeleteConfigDialog from "@/components/serviceManage/serviceConfigManage/ServiceDeleteConfigDialog";
import ServiceRefreshConfigCenterDialog from "@/components/serviceManage/serviceConfigManage/ServiceRefreshConfigCenterDialog";
import {configInfoQuery} from "@/request/configCenter";
import {createTag} from "@/utils/tagCreate";

export default {
    name: "ServiceConfigManage",
    components: {
        ServiceConfigSearch,
        ServiceDeleteConfigDialog,
        ServiceAddOrUpdateConfigDialog,
        ServiceRefreshConfigCenterDialog
    },
    data() {
        return {
            refreshFormRule: {
                name: [{ required: true, message: '微服务名称不能为空', trigger: 'blur' }]
            },
            refreshForm: {
                applicationName: ''
            },
            configCenterForm: {
                application: '',
                profile: '',
                key: '',
                value: ''
            }
        }
    },
    computed: {
        configCenterDataList() {
            return this.$store.state.configCenterDataList;
        },
        configSearchData() {
            return this.$store.state.configCenterSearchData;
        }
    },
    methods: {
        addConfig() {
            this.configCenterForm = {
                id: '',
                application: '',
                profile: '',
                key: '',
                value: ''
            }
            this.$store.commit('changeConfigCenterAddOrUpdateDialogVisible', true);
        },
        handleUpdate(index, row) {
            this.configCenterForm = {
                id: row.id,
                application: row.application,
                profile: row.profile,
                key: row.key,
                value: row.value
            }
            this.$store.commit('changeConfigCenterAddOrUpdateDialogVisible', true);
        },
        handleDelete(index, row) {
            this.configCenterForm = {
                id: row.id,
                application: row.application,
                profile: row.profile,
                key: row.key,
                value: row.value
            }
            this.$store.commit('changeConfigCenterDeleteDialogVisible', true);
        },
        handleCopy(index, row) {
            this.configCenterForm = {
                id: '',
                application: row.application,
                profile: row.profile,
                key: row.key,
                value: row.value
            }
            this.$store.commit('changeConfigCenterAddOrUpdateDialogVisible', true);
        },
        changePage(index, size) {
            this.configSearchData.pageInfo = {
                pageIndex: index,
                pageSize: size
            };
            configInfoQuery(this.configSearchData).then(res => {
                this.$store.commit('changeConfigCenterDataList', res.data);
            })
        },
        refreshConfig() {
            this.$store.commit('changeConfigCenterRefreshDialogVisible', true);
        }
    },
    created() {
        configInfoQuery(this.configSearchData).then(res => {
            this.$store.commit('changeConfigCenterDataList', res.data);
        })
    },
    mounted() {
        createTag(this.$route, this.$store)
    }
}
</script>

<style scoped lang="less">
.apiCacheContainer {
    width: 98%;
    height: 100%;
    margin: 0.2%;
    .searchContainer {
        width: 100%;
        margin-top: 1%;
    }
    .tableContainer {
        margin-left: 1%;
    }
    .insertContainer {
        margin-left: 1%;
        margin-top: 0.7%;
    }
    .refreshContainer {
        margin-top: 5%;
        //去除光标
        caret-color: transparent;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
}
</style>